/**
 * Project: lionbule-java-test
 * 
 * File Created at 2011-10-25
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package javas.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * QueryIp.
 * 2011-10-25 上午10:24:06 @author LionBule
 */
public class QueryIp {

    public static void main(String[] args) throws IOException {
        //testIp1();
        testIp2();
    }
    
    static void testIp1() throws IOException{
        InetAddress inetAddress = InetAddress.getLocalHost();
        String ipStr = inetAddress.getHostAddress();
        System.out.println(ipStr);
        System.out.println(inetAddress.getHostName());
        System.out.println("********************************************************************");
    }
    
    static void testIp2() throws IOException{
        //根据网卡取本机配置的IP    
        Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;

        while (e1.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) e1.nextElement();
            Enumeration<?> e2 = ni.getInetAddresses();
            while (e2.hasMoreElements()) {
                ip = (InetAddress) e2.nextElement();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {
                    System.out.println(ip.getHostAddress());
                } else {
                    ip = null;
                }
            }
        }
    }
}
